package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.BarCodes;
import com.example.entity.BarCodesHistory;
import com.example.mapper.BarCodesHistoryMapper;
import com.example.mapper.BarCodesMapper;
import com.example.service.IBarCodesHistoryService;
import com.example.service.IBarCodesService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigInteger;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2023-03-31
 */
@Service
public class BarCodesServiceImpl extends ServiceImpl<BarCodesMapper, BarCodes> implements IBarCodesService {

    @Resource
    private BarCodesMapper barCodesMapper;

    @Resource
    private IBarCodesHistoryService iBarCodesHistoryService;

    @Transactional
    public boolean selectBarCodes() {
        List<BarCodesHistory> barCodesHistories;

        // 获取需要迁移的数据
        barCodesHistories = barCodesMapper.selectMigrationData();

        if (ObjectUtils.isEmpty(barCodesHistories)) {
            return false;
        }

        // 保存数据
        iBarCodesHistoryService.saveBatch(barCodesHistories);

        // 删除数据
        List<Long> ids = new ArrayList<>();
        barCodesHistories.forEach(p -> {
            ids.add(p.getJid());
        });
        barCodesMapper.deleteBatchIds(ids);

        barCodesHistories = barCodesMapper.selectMigrationData();

        return ObjectUtils.isNotEmpty(barCodesHistories);
    }
}
